import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { getMapAPI } from '@/api/map.js'
import { setToken, removeToken } from '@/utils/auth'


// const baseUrl = config.baseUrl

const map = {
  state: {
  
 // 字典
	map:storage.get(constant.map)
  },

  mutations: {
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      storage.set(constant.roles, roles)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.permissions, permissions)
    },
	setMapMU: (state, map) => {
	  state.map = map
	  storage.set(constant.map, map)
	}
  },

  actions: {
   

    // 获取用户信息
    getmapAC({ commit, state }) {
      return new Promise(async (resolve, reject) => {
		  try{
		  	const {data} = await  getMapAPI()
		  	console.log('map',data)
			if (!data) {
			        return;
			      }
			      // 设置数据
			      const dictDataMap = {}
			    data.forEach(dictData => {
			        // 获得 dictType 层级
			        const enumValueObj = dictDataMap[dictData.dictType]
			        if (!enumValueObj) {
			          dictDataMap[dictData.dictType] = []
			        }
			        // 处理 dictValue 层级
			        dictDataMap[dictData.dictType].push({
			          value: dictData.value,
			          text: dictData.label,
			          colorType: dictData.colorType,
			          cssClass: dictData.cssClass,
			        })
			      })
			commit('setMapMU',dictDataMap )
		  	 resolve()
		  }catch(e){
			  reject(e)
		  	//TODO handle the exception
		  }
    
      })
    },

  
  }
}

export default map
